package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Visitor;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/05
 */
@Service
public class VisitorApiImpl implements VisitorApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitor> findVisitorListByCount(long userId, int n) {
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .limit(n);

        return mongoTemplate.find(query, Visitor.class);
    }

    @Override
    public List<Visitor> findVisitorListByStartTime(long userId, long startTime) {
        Query query = new Query(Criteria.where("userId").is(userId).and("date").gt(startTime))
                .with(Sort.by(Sort.Order.desc("date")));
        return mongoTemplate.find(query, Visitor.class);
    }
}
